﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cashier_Service;
using Cashier.utils;
using System.Collections;
using Cashier_Model;
using Cashier.usercontrols;
using Cashier_Interface;

namespace Cashier.goods
{
    public partial class WarehouseAddForm : FormExt
    {
        private IReloadData iReloadData;
        private Object iParam;
        private long warehouseid;
        private Warehouse warehouse;
        private ChinacityService chinacityService;
        private WarehouseService warehouseService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="iReloadData"></param>
        /// <param name="?"></param>
        /// <param name="iParam"></param>
        /// <param name="warehouseid"></param>
        public WarehouseAddForm(IReloadData iReloadData, Object iParam, long warehouseid)
        {
            InitializeComponent();
            this.iReloadData = iReloadData;
            this.iParam = iParam;
            this.warehouseid = warehouseid;
            this.chinacityService = new ChinacityService();
            this.warehouseService = new WarehouseService();

            this.InitialCombox(comboBox_province, "0");
            this.InitialFormData();
        }

        /// <summary>
        /// 填充下拉框数据
        /// </summary>
        /// <param name="comboBox"></param>
        /// <param name="parent"></param>
        private void InitialCombox(ComboBox comboBox, String parent)
        {
            try
            {
                DataTable dt = chinacityService.GetChinacityTable(parent);
                if (dt != null && dt.Rows.Count > 0)
                {
                    comboBox.DataSource = dt;
                    comboBox.DisplayMember = "name";
                    comboBox.ValueMember = "code";
                    comboBox.SelectedIndex = 1;
                    comboBox.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        /// <summary>
        /// 初始化表单数据
        /// </summary>
        private void InitialFormData()
        {
            try
            {
                if (warehouseid > 0)
                {
                    warehouse = warehouseService.GetWarehouse(warehouseid, null);
                    if (warehouse != null)
                    {
                        FormUtil.InitialFormData(warehouse, this, 1);
                        comboBox_province.Text = warehouse.Province;
                        comboBox_city.Text = warehouse.City;
                        comboBox_area.Text = warehouse.Area;
                    }
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        #region 控件事件

        /// <summary>
        /// 省下拉控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox_province_SelectedIndexChanged(object sender, EventArgs e)
        {
            String value = comboBox_province.SelectedValue.ToString();
            if (value.Length == 6)
            {
                this.InitialCombox(comboBox_city, value);
            }
        }

        /// <summary>
        /// 市下拉控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox_city_SelectedIndexChanged(object sender, EventArgs e)
        {
            String value = comboBox_city.SelectedValue.ToString();
            if (value.Length == 6)
            {
                this.InitialCombox(comboBox_area, value);
            }
        }

        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_save_Click(object sender, EventArgs e)
        {
            try
            {
                ArrayList list = new ArrayList();
                FormUtil.GetValidateControlsNotPassed(this, list, 1);
                if (list.Count > 0)
                    return;

                if (warehouse == null)
                {
                    warehouse = new Warehouse();
                    warehouse.Createtime = DateTime.Now;
                }
                FormUtil.GetFormData(warehouse, this, 1);

                bool issuccess = false;
                if (warehouse.Id == 0)
                {
                    long id = warehouseService.InsertWarehouse(warehouse);
                    if (id > 0)
                        issuccess = true;
                }
                else
                {
                    int flag = warehouseService.UpdateWarehouse(warehouse);
                    if (flag > 0)
                        issuccess = true;
                }

                if (issuccess)
                {
                    DialogResult result = MessageBox.Show("保存成功，是否添加新记录！", "提示", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        warehouse = null;
                        FormUtil.ClearFormData(this, 1);
                    }
                    else
                    {
                        this.Close();
                    }
                    this.iReloadData.ReloadDataGridViewData(iParam);
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion


        /// <summary>
        /// ESC键退出窗体
        /// </summary>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessDialogKey(Keys keyData)
        {
            bool b = base.ProcessDialogKey(keyData);
            if (keyData == Keys.Escape)
            {
                b = false;
                this.Close();
            }
            return b;
        }
    }
}
